package observer;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @Author liangliangzhao
 * @data 2023/4/18 17:42
 */

public class WeatherData implements Subject{
    private float temperatrue;
    private float pressure;
    private float humidity;
    private ArrayList<Observer> observers;//观察者集合
    public WeatherData(){
        observers = new ArrayList<Observer>();
    }
    public float getTemperatrue() {
        return temperatrue;
    }
    public void setTemperatrue(float twemperatrue) {
        this.temperatrue = temperatrue;
    }
    public float getPressure() {
        return pressure;
    }
    public void setPressure(float pressure) {
        this.pressure = pressure;
    }
    public float getHumidity() {
        return humidity;
    }
    public void setHumidity(float humidity) {
        this.humidity = humidity;
    }
    public void dataChange(){
        notifyObservers();
    }
    public void setData(float temperatrue,float pressure,float humidity){
        this.temperatrue = temperatrue;
        this.pressure = pressure;
        this.humidity = humidity;
        dataChange();
    }
    public void registerObserver(Observer o){
        observers.add(o);
    }//注册
    public void removeObserver(Observer o){//移除
        if(observers.contains(o)) observers.remove(o);
    }
    //遍历所有的观察者 并通知
    public void notifyObservers(){
        for (int i = 0; i < observers.size(); i++) {
            observers.get(i).update(this.temperatrue,this.pressure,this.humidity);
        }
    }

}
